﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using MiniWeb.Core.Entities;
using MiniWeb.Task;
using MiniWeb.Web.Controllers.Admin.AccountsInfoManager.ViewModels;
using WFramework;
using WFramework.Mappers;
using WFramework.MvcPager;
using WFramework.Utility;

namespace MiniWeb.Web.Controllers.Admin.AccountsInfoManager
{

    public class AccountsInfoManagerController : BaseController
    {
        private readonly AccountsInfoTask _accountsinfoTask = new AccountsInfoTask();

        public ActionResult List(string account, int pageIndex = 1, int pageSize = 20)
        {
            var pagedList = _accountsinfoTask.GetPagedList(account,pageIndex, pageSize);



            var model = new PagedList<AccountsInfo>(pagedList.ToList(), pageIndex, pageSize, pagedList.TotalItemCount);

            return View(model);
        }


        public ActionResult Save(int? id)
        {


            var model = new SaveAccountsInfo();

            if (id != null)
            {
                var item = _accountsinfoTask.GetById((int)id);
                if (item == null)
                    return AlertMsg("参数错误", HttpContext.Request.UrlReferrer.PathAndQuery);

                model = EntityMapper.Map<AccountsInfo, SaveAccountsInfo>(item);
            }
            model.Password = "";
            return View(model);
        }

        [HttpPost]
        public ActionResult Save(SaveAccountsInfo savemodel)
        {


            if (ModelState.IsValid)
            {
                string errMsg, savePath = string.Empty;
                var dict = new Dictionary<string, string>();
                if (savemodel.ID == null)
                {

                    foreach (var key in Request.Files.AllKeys)
                    {
                        if (Request.Files[key] == null || Request.Files[key].ContentLength == 0)
                        {
                            ModelState.AddModelError(key, "请上传");
                            return View(savemodel);
                        }

                        if (!UploadService.UploadFile(Request.Files[key], out savePath, out errMsg))
                        {
                            return AlertMsg(errMsg, HttpContext.Request.UrlReferrer.PathAndQuery);
                        }
                        dict.Add(key, savePath);
                    }

                   
                }
                else
                {
                    var model = _accountsinfoTask.GetById((int)savemodel.ID);

                    foreach (var key in Request.Files.AllKeys.Where(key => Request.Files[key] != null && Request.Files[key].ContentLength > 0))
                    {

                        if (!UploadService.UploadFile(Request.Files[key], out savePath, out errMsg))
                        {
                            return AlertMsg(errMsg, HttpContext.Request.UrlReferrer.PathAndQuery);
                        }
                        dict.Add(key, savePath);
                    }

                    if (model == null)
                        return AlertMsg("参数错误", HttpContext.Request.UrlReferrer.PathAndQuery);

                    model.Account = savemodel.Account;
                    if(!string.IsNullOrEmpty(savemodel.Password))
                    {
                        model.Password = CryptTools.HashPassword(savemodel.Password);
                    }
                    model.Phone = savemodel.Phone;
                    model.Email = savemodel.Email;
                    //model.HeadImg = dict.ContainsKey("HeadImg") ? dict["HeadImg"] : model.HeadImg;


                    _accountsinfoTask.Update(model);
                }
                return AlertMsg("保存成功", HttpContext.Request.UrlReferrer.PathAndQuery);

            }

            ////////////////////////////////////////


            return View(savemodel);
        }



        #region 删除用户信息 Delete

        [HttpPost]
        public void Delete(int id)
        {
            _accountsinfoTask.Delete(id);
        }

        #endregion
    }
}